#define OptimisticKeyManager
//#define OleDb
//#define Oracle

using System;
using System.Collections;
using System.Reflection;
using Eldorado.Relational;
using Eldorado.Relational.RelationalSource;
using Eldorado.Relational.SqlDriver;
using Eldorado.Data;
#if OleDb || Oracle
using Eldorado.Data.OleDb;
#else
using Eldorado.Data.SqlClient;
#endif
using Eldorado.Object;
using Eldorado.Object.Relational;
using ProjectManagement.DataAccess;
using NUnit.Framework;

namespace Northwind.NUnit
{
	public class BaseTest : Assertion
	{
		public static string ConnectionString;
		public static IObjectDomain Domain;

		static BaseTest()
		{
			IDataSource datasource = null;
#if Oracle
			ConnectionString = "Provider=MSDAORA.1;Password=dlb;User ID=dlb;Data Source=dlb";
			datasource = new OleDbDataSource(ConnectionString);
#else
			Eldorado.Data.SqlClient.FrmSqlConnection f = new Eldorado.Data.SqlClient.FrmSqlConnection();
			f.Catalog = "ProjectManagement";
			if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)  
			{
#if OleDb
				ConnectionString = "Provider=SQLOLEDB;Initial Catalog=" + f.Catalog + ";" + f.ConnectionString;
				datasource = new OleDbDataSource(ConnectionString);
#else
				ConnectionString = "Initial Catalog=" + f.Catalog + ";" + f.ConnectionString;
				datasource = new SqlDataSource(ConnectionString);
#endif
			}
#endif
			if (datasource == null) 
			{
				System.Diagnostics.Trace.WriteLine("cannot connect to database");
				System.Threading.Thread.CurrentThread.Abort();
			}

			IDictionary extendedProperties = new Hashtable();
			extendedProperties["DataSource"] = datasource;
#if OptimisticKeyManager
#else
			KeyManager = new PessimisticKeyManager();
			KeyManager.ExtendedProperties["OidRange"] = 10;
#endif
#if Oracle
			CustomerDemographicsDomain = new ObjectDomain(Assembly.Load("Northwind.DataAccess"), "Northwind.DataAccess.Map.Oracle.xml", extendedProperties);
#else
			Domain = new ObjectDomain(Assembly.Load("ProjectManagement.DataAccess"), "ProjectManagement.DataAccess.Map.SqlServer.xml", extendedProperties);
#endif
		}

		public IObjectContext CreateContext()
		{
#if Oracle
			IDbRelationalSource source = new OleDbRelationalSource(ConnectionString, new PlSql8Driver());
#elif OleDb
			IDbRelationalSource source = new OleDbRelationalSource(ConnectionString, new TransactSqlDriver());
#else
			IDbRelationalSource source = new SqlRelationalSource(ConnectionString);
#endif
			return new RelationalObjectContext(Domain, source);
		}

		public Employee CreateEmployee(IObjectContext context)
		{
			Employee result = (Employee)context.LocalStorage.Create(typeof(Employee));
			return result;
		}

		public Manager CreateManager(IObjectContext context)
		{
			Manager result = (Manager)context.LocalStorage.Create(typeof(Manager));
			return result;
		}

		public Project CreateProject(IObjectContext context)
		{
			Project result = (Project)context.LocalStorage.Create(typeof(Project));
			return result;
		}
	}
}
